package com.shelpe.services.sinbad.entity.jpa;

import java.io.Serializable;
import java.util.Date;

import javax.persistence.AttributeConverter;
import javax.persistence.Column;
import javax.persistence.Convert;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.IdClass;

import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonFormat.Shape;
import com.shelpe.services.sinbad.entity.jpa.Campaign.Area;
import com.shelpe.services.sinbad.entity.jpa.Campaign.ScheduleObject;
import com.shelpe.services.sinbad.entity.jpa.CampaignHistory.CampaignHistoryPK;
import com.shelpe.services.sinbad.service.utils.EntityUtils.Boolean2StringConverter;
import com.shelpe.services.sinbad.service.utils.EntityUtils.DriveTypeConverter;
import com.shelpe.services.sinbad.service.utils.EntityUtils.OnlineStatusConverter;
import com.shelpe.services.sinbad.utils.CampaignUtils;

import lombok.Data;

@Entity(name = "campaigns_history")
@IdClass(CampaignHistoryPK.class)
@Data
public class CampaignHistory {
	@Id
	@Column(name = "campaign_id")
	private long autoID;

	@Id
	@JsonFormat(shape = Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
	private Date createTime;
	@Id
	@Column(name = "action_type")
	private String actionType;

	@Column(name = "user_id")
	private long userID;
	private String nick;
	private String title;
	private int budget;
	@Convert(converter = Boolean2StringConverter.class)
	private boolean useSmooth;
	private String incrementalPrice;
	private String platform;
	@Convert(converter = DriveTypeConverter.class)
	private DriveType autoType = DriveType.auto;
	@Column(name = "`limit`")
	private int limit;
	@Convert(converter = OnlineStatusConverter.class)
	private OnlineStatus onlineStatus;
	@Convert(converter = OnlineStatusConverter.class)
	private OnlineStatus settleStatus;
	private String settleReason;
	private String schedule;
	@Convert(converter = AreaConverter.class)
	private Area area;

	@Data
	public static class CampaignHistoryPK implements Serializable {
		private static final long serialVersionUID = 1L;
		@Column(name = "campaign_id")
		private long autoID;

		@Column(name = "action_type")
		private String actionType;

		@JsonFormat(shape = Shape.STRING, pattern = "yyyy-MM-dd HH:mm:ss")
		private Date createTime;
	}

	public static class AreaConverter implements AttributeConverter<Area, String> {

		@Override
		public String convertToDatabaseColumn(Area attribute) {
			return CampaignUtils.fromArea(attribute);
		}

		@Override
		public Area convertToEntityAttribute(String dbData) {
			return CampaignUtils.toArea(dbData);
		}
	}

	public static class ScheduleConverter implements AttributeConverter<ScheduleObject, String> {

		@Override
		public String convertToDatabaseColumn(ScheduleObject attribute) {
			return CampaignUtils.fromScheduleObject(attribute);
		}

		@Override
		public ScheduleObject convertToEntityAttribute(String dbData) {
			return CampaignUtils.toScheduleObject(dbData);
		}
	}
}
